package soap;

import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.AttachmentPart;
//import javax.activation.DataHandler;
//import javax.activation.FileDataSource;
import java.io.FileInputStream;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import java.io.*;
import javax.xml.transform.stream.StreamResult;
import java.util.Iterator;




/**
 * SClient
 * A soap client mostly fished out of the web
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 * 
 */
public class SClient {

	/**
	 * Construtor vazio SClient
	 */
	public SClient() {
	}
	
	
	/**
	 * soapRequest
	 * @param message mensagem
	 * @param outFileName outFileName
	 */
	public void soapRequest(String message, String outFileName) {

		System.out.println("Inicio do SClient -> soapRequest ");
		try {
			SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
			SOAPConnection conn = scf.createConnection();

			// Create message
			MessageFactory mf = MessageFactory.newInstance();
			SOAPMessage msg = mf.createMessage();

			// Object for message parts
			SOAPPart sp = msg.getSOAPPart();
			StreamSource prepMsg = new StreamSource(
					new FileInputStream(message));
			sp.setContent(prepMsg);
			// sp.setTextContent(message);
			
			// Save message
			msg.saveChanges();

			// View input
			System.out.println("\n Soap request:\n");
			System.out.flush();
			msg.writeTo(System.out);
			System.out.println();

			// Send
			String urlval = "http://ops.espacenet.com/OpenPatentServices/webService";
			SOAPMessage rp = conn.call(msg, urlval);
			// conn.call(msg, urlval).setProperty(arg0, arg1);

			// View the output
			System.out.println("\nXML response\n");

			// Create transformer
			TransformerFactory tff = TransformerFactory.newInstance();
			Transformer tf = tff.newTransformer();

			// Get reply content , complete soap message
			Source sc = rp.getSOAPPart().getContent();

			// cria arquivo de saida
			PrintStream out = new PrintStream(outFileName);

			// Set output transformation
			// StreamResult result = new StreamResult(System.out);
			StreamResult result = new StreamResult(out);
			tf.transform(sc, result);
			// System.out.println();
			out.println();
			// System.out.println(reply);
			SOAPEnvelope responseSe = rp.getSOAPPart().getEnvelope();
			SOAPBody responseBody = responseSe.getBody();
			// Now extract the attachments
			if (!responseBody.hasFault()) {
				int numOfAttachments = rp.countAttachments();
				System.out.println("\n\nNumber of attachments in message = "
						+ numOfAttachments);
				Iterator<?> iterator = rp.getAttachments();
				if (iterator.hasNext()) {
					AttachmentPart attached = (AttachmentPart) iterator.next();
					String id = attached.getContentId();
					String type = attached.getContentType();
					System.out.println("***Attachment client " + id
							+ " has content type " + type);
					Object content = attached.getContent();
					System.out.println("Attachment contains:\n" + content);
				}
			}
			// Close connection
			conn.close();
		}
		catch (UnsupportedOperationException e1) {
			e1.printStackTrace();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println("Fim do SClient -> soapRequest ");
	}
	
	
}